package com.ruoyi.backend.controller;

import com.ruoyi.backend.dto.UavInfoUpdateDTO;
import com.ruoyi.backend.dto.UavUploadDTO;
import com.ruoyi.backend.pojo.Uav;
import com.ruoyi.backend.service.UavsService;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.enums.DataSourceType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Api(tags = "无人机数据管理相关接口", value = "(PostgreSQL主导执行)全部接口已完成测试")
@RequestMapping("/backend/uavs")
@RestController
public class UavsController {

    @Autowired()
    private UavsService uavsService;

    @DataSource(DataSourceType.SLAVE)
    @ApiOperation(value = "上传无人机数据接口")
    @PostMapping("/upload")
    public R<String> upload(@ModelAttribute @Validated(UavUploadDTO.Upload.class) UavUploadDTO uavUploadDTO) throws IOException {

        uavsService.upload(uavUploadDTO);
        return R.ok("无人机数据上传成功");
    }

    @DataSource(DataSourceType.SLAVE)
    @ApiOperation(value = "查询无人机数据接口")
    @GetMapping("/search")
    public R<List<Uav>> search(@RequestParam (name = "mineId", required = false) String mineId,
                         @RequestParam (name = "uavName", required = false) String uavName,
                         @RequestParam (name = "startTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startTime,
                         @RequestParam (name = "endTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endTime,
                         @RequestParam (name = "dataType", required = false) String dataType,
                         @RequestParam (name = "saveType", required = false) String saveType
                         ) {

        List<Uav> uavList = uavsService.search(mineId, uavName, startTime, endTime, dataType, saveType);
        return R.ok(uavList, "无人机数据查询成功");
    }

    @DataSource(DataSourceType.SLAVE)
    @ApiOperation(value = "删除无人机数据接口")
    @DeleteMapping("/delete")
    public R<String> delete(@RequestParam ("id") @NotNull(message = "无人机ID不能为空") String uavId,
                            @RequestParam ("uavTime") @NotNull(message = "无人机时间不能为空") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate uavTime) {

        uavsService.delete(uavId, uavTime);
        return R.ok("无人机数据删除成功");
    }

    @DataSource(DataSourceType.SLAVE)
    @ApiOperation(value = "更新无人机数据接口")
    @PutMapping("/update")
    public R<String> update(@RequestBody @Validated(UavInfoUpdateDTO.Update.class) UavInfoUpdateDTO uavInfoUpdateDTO) {

        uavsService.update(uavInfoUpdateDTO);
        return R.ok("无人机数据更新成功");
    }
}
